/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject.inputQuestion;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import ru.irenproject.FormulationSupplier;
import ru.irenproject.Question;
import ru.irenproject.TestCheck;
import ru.irenproject.pad.Pad;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;

import com.google.protobuf.ByteString;

public final class InputQuestion extends AbstractThing
    implements Question, FormulationSupplier {
  public enum ChangeEvent implements Event { INSTANCE }

  public static final int MAX_PATTERNS = 20;

  @Stash private InputQuestionProtos.InputQuestion.Builder $;

  @Inject InputQuestion() {}

  @Override protected void initialize() {
    Pad formulation = store().create(Pad.class);
    $.setFormulation(formulation.id());
  }

  @Override public Pad formulation() {
    return get($.getFormulation(), Pad.class);
  }

  public List<Pattern> patternList() {
    return store().getBatch($.getPatternList(), Pattern.class);
  }

  public Pattern patterns(int index) {
    return get($.getPattern(index), Pattern.class);
  }

  private <T extends AbstractPattern> T addPattern(Class<? extends T> cls) {
    TestCheck.input(patternCount() < MAX_PATTERNS);

    T res = store().create(cls);
    $.addPattern(res.id());
    post(ChangeEvent.INSTANCE);
    return res;
  }

  public TextPattern addTextPattern() {
    return addPattern(TextPattern.class);
  }

  public RegexpPattern addRegexpPattern() {
    return addPattern(RegexpPattern.class);
  }

  public int patternCount() {
    return $.getPatternCount();
  }

  public void deletePattern(int index) {
    checkPatternIndex(index);

    List<ByteString> ids = new ArrayList<>($.getPatternList());
    ids.remove(index);
    $.clearPattern().addAllPattern(ids);

    post(ChangeEvent.INSTANCE);
  }

  private void checkPatternIndex(int index) {
    TestCheck.input(index >= 0);
    TestCheck.input(index < patternCount());
  }

  public void movePattern(int index, boolean forward) {
    checkPatternIndex(index);
    int newIndex = index + (forward ? 1 : -1);
    checkPatternIndex(newIndex);

    ByteString temp = $.getPattern(index);
    $.setPattern(index, $.getPattern(newIndex));
    $.setPattern(newIndex, temp);
    post(ChangeEvent.INSTANCE);
  }

  @Override public void setDefaults(String initialText) {
    formulation().appendText(initialText);
    addTextPattern();
  }
}
